package com.example.adapter;

import java.util.ArrayList;
import java.util.List;

import com.example.dianping.R;
import com.example.entity.Goods;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.picasso.Picasso;

import android.database.DataSetObserver;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

public class TuanListAdapter extends BaseAdapter {

	private List<Goods> datas = new ArrayList<Goods>();

	public TuanListAdapter(List<Goods> datas) {
		this.datas = datas;
	}

	@Override
	public int getCount() {
		return datas.size();
	}

	@Override
	public Object getItem(int position) {
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		if (convertView == null) {
			viewHolder = new ViewHolder();
//			System.out.println(convertView+"----");
			convertView = LayoutInflater.from(parent.getContext()).inflate(
					R.layout.fragment_tuan_goods_listview_item, null);
//			System.out.println(convertView+"+++++");
			ViewUtils.inject(viewHolder, convertView);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		Goods goods = datas.get(position);

		viewHolder.tv_tuan_goods_listview_item_title.setText(goods
				.getSortTitle());
		viewHolder.tv_tuan_goods_listview_item_content
				.setText(goods.getTitle());

		viewHolder.tv_tuan_goods_listview_item_old_price.setText("￥"
				+ goods.getPrice());
		viewHolder.tv_tuan_goods_listview_item_sell_num.setText(goods
				.getBought() + "份");

		// 中划线样式处理
		StringBuffer sbf = new StringBuffer("￥" + goods.getValue());
		SpannableString spannable = new SpannableString(sbf);
		spannable.setSpan(new StrikethroughSpan(), 0, sbf.length(),
				Spanned.SPAN_INCLUSIVE_INCLUSIVE);

		viewHolder.tv_tuan_goods_listview_item_old_price.setText(spannable);

		// Picasso框架加载图片显示 避免图片错位以及OOM
		Picasso.with(parent.getContext()).load(goods.getImgUrl())
				.placeholder(R.drawable.empty_photo)
				.into(viewHolder.iv_tuan_goods_listview_item_photo);
		return convertView;
	}

	public class ViewHolder {
		@ViewInject(R.id.iv_tuan_goods_listview_item_photo)
		ImageView iv_tuan_goods_listview_item_photo;
		@ViewInject(R.id.tv_tuan_goods_listview_item_title)
		TextView tv_tuan_goods_listview_item_title;
		@ViewInject(R.id.tv_tuan_goods_listview_item_content)
		TextView tv_tuan_goods_listview_item_content;
		@ViewInject(R.id.tv_tuan_goods_listview_item_now_price)
		TextView tv_tuan_goods_listview_item_now_price;
		@ViewInject(R.id.tv_tuan_goods_listview_item_old_price)
		TextView tv_tuan_goods_listview_item_old_price;
		@ViewInject(R.id.tv_tuan_goods_listview_item_sell_num)
		TextView tv_tuan_goods_listview_item_sell_num;
	}

}
